using System;
using System.Collections.Generic;
using Function.Preferences;
using UnityEngine;

namespace Function.HotUpdate
{
    [CreateAssetMenu(fileName = "HotLang", menuName = "ScriptableObject/HotLang", order = 0)]
    public class HotLang : ScriptableObject
    {
        [Serializable]
        public class HotData
        {
            public string _local;
            public string _text;
            public int _key;
        }

        public List<HotData> _hotLang;

        private Dictionary<int, Dictionary<string, string>> _hotLangMap =
            new Dictionary<int, Dictionary<string, string>>();

        public void Init()
        {
            foreach (var lang in _hotLang)
            {
                if (!_hotLangMap.TryGetValue(lang._key, out var hotD))
                {
                    hotD = new Dictionary<string, string>();
                    hotD.Add(lang._local, lang._text);
                    _hotLangMap.Add(lang._key, hotD);
                }
                else
                {
                    hotD.Add(lang._local, lang._text);
                }
            }
        }

        public string GetText(int key, bool isTurn = true)
        {
            if (_hotLangMap.Count == 0)
            {
                Init();
            }

            string language = GetCurLanguage(key);
            //阿拉伯语
            if (language == "ar_ae")
            {
                if (isTurn)
                {
                    //AOTEasyArabicCore.CorrectString
                    return _hotLangMap[key][language];
                }

                else
                {
                    return _hotLangMap[key][language];
                }
            }
            else
            {
                return _hotLangMap[key][language];
            }
        }

        public string GetCurLanguage(int key)
        {
            string language = PlayerPref.GetString("locale", "en_us", true);
            _hotLangMap.TryGetValue(key, out var hotD);
            if (!hotD.TryGetValue(language, out var text))
            {
                language = "en_us";
            }

            return language;
        }
    }
}